﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelReport
{
    // graphs the luns' response times
    class ResponseTimeReport
    {
        object misValue;
        Excel.Application excelApp;
        Excel.Workbook workbook;

        public ResponseTimeReport()
        {
            misValue = System.Reflection.Missing.Value;
            excelApp = new Excel.Application();
            // Make the object visible.
            excelApp.Visible = true;
            workbook = excelApp.Workbooks.Add(1);
            responseTimeHelper();
        }

        public void responseTimeHelper(){
            StreamReader iter1 = new StreamReader("../../../DataBase/OutputFiles/LunResponseTimeReport1.txt");
            StreamReader iter2 = new StreamReader("../../../DataBase/OutputFiles/LunResponseTimeReport2.txt");
            StreamReader iter3 = new StreamReader("../../../DataBase/OutputFiles/LunResponseTimeReport3.txt");
            //StreamReader iter4 = new StreamReader("../../../DataBase/OutputFiles/LunResponseTimeReport4.txt");
            TextWriter group1 = new StreamWriter("../../../DataBase/OutputFiles/LunResponseTimeGroup1.txt");
            TextWriter group2 = new StreamWriter("../../../DataBase/OutputFiles/LunResponseTimeGroup2.txt");
            TextWriter group3 = new StreamWriter("../../../DataBase/OutputFiles/LunResponseTimeGroup3.txt");
            String line;
            char[] delimiters = new char[] { '\t' };

            line=iter1.ReadLine();
            string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            group1.WriteLine("desired response time:\t" + parts[1]);
            line = iter1.ReadLine();
            parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            group2.WriteLine("desired response time:\t" + parts[1]);
            line = iter1.ReadLine();
            parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            group3.WriteLine("desired response time:\t" + parts[1]);
            line = iter1.ReadLine();
            int num_time_slices = Convert.ToInt32(line);
            for (int i = 0; i < 4; i++)
            {
                iter2.ReadLine();
                iter3.ReadLine();
                //iter4.ReadLine();
            }

            group1.WriteLine(num_time_slices);
            group2.WriteLine(num_time_slices);
            group3.WriteLine(num_time_slices);

            for (int i = 0; i < num_time_slices; i++)
            {
                line = iter1.ReadLine();
                iter2.ReadLine();
                iter3.ReadLine();
                //line = iter4.ReadLine();
                group1.WriteLine(line);
                group2.WriteLine(line);
                group3.WriteLine(line);

                line = iter1.ReadLine();
                group1.WriteLine(line);
                line = iter1.ReadLine();
                group2.WriteLine(line);
                line = iter1.ReadLine();
                group3.WriteLine(line);

                line = iter2.ReadLine();
                group1.WriteLine(line);
                line = iter2.ReadLine();
                group2.WriteLine(line);
                line = iter2.ReadLine();
                group3.WriteLine(line);

                line = iter3.ReadLine();
                group1.WriteLine(line);
                line = iter3.ReadLine();
                group2.WriteLine(line);
                line = iter3.ReadLine();
                group3.WriteLine(line);
                
               /* line = iter4.ReadLine();
                group1.WriteLine(line);
                line = iter4.ReadLine();
                group2.WriteLine(line);
                line = iter4.ReadLine();
                group3.WriteLine(line);*/
            }

            iter1.Close();
            iter2.Close();
            iter3.Close();
           // iter4.Close();
            group1.Close();
            group2.Close();
            group3.Close();
        }


        /* file will be:
         * group1 desired response time:    x milliseconds
         * group2 desired response time:    y milliseconds
         * group3 desired response time:    z milliseconds
         * i - num of time slices
         * time
         * group1: ssd util
         * group2:  fc util
         * group3: sata util
        */
        public void RTReport()
        {
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = "ResponseTime";//Group"+iter.ToString();
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 170, 750, 300);
            Excel.Chart chartPage = myChart.Chart;
            var seriesCol = chartPage.SeriesCollection(misValue);
            chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
            //reading from file

            int count=0;
            double[] iteration1=null, iteration2=null, iteration3=null;// iteration4;
            int[] times = null;
            int index = 0;
            for (int iter=1; iter<4; iter++){
                //"../../../../DataBase/DataBase/OutputFiles/LunResponseTimeGroup" + iter + ".txt"
                using (StreamReader sr = new StreamReader("../../../DataBase/OutputFiles/LunResponseTimeGroup" + iter + ".txt"))
                {
                    String line;
                    char[] delimiters = new char[] { '\t' };
                    line = sr.ReadLine();
                    string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    string desired = parts[1];
                    line = sr.ReadLine();
                    count = Convert.ToInt32(line);
                    if (iter==1){
                        iteration1 = new double[count*3];
                        iteration2 = new double[count*3];
                        iteration3 = new double[count*3];
                        times = new int[count*3];
                    }

                    while (index < count*iter)
                    {
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        //Console.WriteLine("time: " + Convert.ToInt32(parts[1]));
                        times[index] = Convert.ToInt32(parts[1]);
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        iteration1[index] = Convert.ToDouble(parts[1]);
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        iteration2[index] = Convert.ToDouble(parts[1]);
                        line = sr.ReadLine();
                        parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        iteration3[index] = Convert.ToDouble(parts[1]);
                        /*       line = sr.ReadLine();
                               parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                               iteration4[index] = Convert.ToDouble(parts[1]);
                          */
                        index++;
                    }
                }
            }
            int series = 1;
            string col;
            for (int n = 0; n < 3; n++)
            {
                if(n==0)
                    worksheet.Cells[1, 1] = "Times";
                worksheet.Cells[2+(n*3), 1] = "No Priority - Group "+(n+1);
                worksheet.Cells[3+(n*3), 1] = "Static Priority - Group "+(n+1);
                worksheet.Cells[4+(n*3), 1] = "Dynamic Priority - Group "+(n+1);
                //worksheet.Cells[5, 1] = "Optimal Static Priority";
            }
            for (int n = 0; n < 3; n++)
            {
                for (int k = 0; k < count; k++)
                {
                    if (n == 0)
                    {
                        worksheet.Cells[1, k + 2] = times[k];
                    }
                    worksheet.Cells[2 + (n * 3), k + 2] = iteration1[(n * count) + k];
                    worksheet.Cells[3 + (n * 3), k + 2] = iteration2[(n * count) + k];
                    worksheet.Cells[4 + (n * 3), k + 2] = iteration3[(n * count) + k];
                    // worksheet.Cells[5+i, i + 2] = iteration4[i];
                }
            }

            Excel.Range chartRange1, chartRange2;
            char cColumn = 'A';
            for (int i = 0; i < count && i<22; i++)
                cColumn++;
            col = cColumn.ToString();

            chartRange2 = worksheet.Range["B1", col + "1"];
            for (int k = 0; k < 3; k++)
            {
                chartRange1 = worksheet.Range["B" + Convert.ToString(2 + (k * 3)), col + Convert.ToString(2 + (k * 3))];
                //cColumn++;
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "No Priority - Group "+ (k+1);
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B" + Convert.ToString(3 + (k * 3)), col + Convert.ToString(3 + (k * 3))];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "Static Priority - Group "+ (k+1);
                seriesCol(series).XValues = chartRange2;
                series++;
                chartRange1 = worksheet.Range["B" + Convert.ToString(4 + (k * 3)), col + Convert.ToString(4 + (k * 3))];
                seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                seriesCol(series).Name = "Dynamic Priority - Group "+ (k+1);
                seriesCol(series).XValues = chartRange2;
                // add when there is a forth iteration
                    series++;
                /*    chartRange1 = worksheet.Range["B5", col + "5"];
                    seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                    seriesCol(series).Name = "Optimal Static Priority";
                    seriesCol(series).XValues = chartRange2;*/
            }
        }

        
        private void dump(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine(ex);
                Environment.Exit(-1);
            }
            finally
            {
                GC.Collect();
            }
        }

    }
}
